
import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.JTable;
import javax.swing.table.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Jobson
 */
public class ConsultaAluno extends javax.swing.JInternalFrame {

    /**
     * Creates new form ConsultaAluno
     */
    public ConsultaAluno(Aluno aluno, LinkedList<Disciplina> disciplinas) {
        initComponents();
        labelNome.setText(labelNome.getText() + aluno.getNome());
        labelCurso.setText(labelCurso.getText() + aluno.getCurso());
        labelAnoPeriodo.setText(labelAnoPeriodo.getText() + aluno.getAnoIng() + "." + aluno.getPeriodo());
        cBoxDisc.setEnabled(false);
        for (Disciplina d : disciplinas) {
            cBoxDisc.addItem(d.getNome());
        }
        cBoxDisc.setSelectedIndex(-1);
        cBoxDisc.setEnabled(true);
        this.disciplinas = disciplinas;
        this.aluno = aluno;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelNome = new javax.swing.JLabel();
        labelCurso = new javax.swing.JLabel();
        labelAnoPeriodo = new javax.swing.JLabel();
        cBoxDisc = new javax.swing.JComboBox();
        labelNota1 = new javax.swing.JLabel();
        labelNota2 = new javax.swing.JLabel();
        labelNota3 = new javax.swing.JLabel();
        labelNota4 = new javax.swing.JLabel();
        labelFinal = new javax.swing.JLabel();
        labelFaltas = new javax.swing.JLabel();
        labelCH = new javax.swing.JLabel();
        labelMedia = new javax.swing.JLabel();
        labelSituacao = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Relatório do Aluno");

        labelNome.setText("Nome: ");

        labelCurso.setText("Curso: ");

        labelAnoPeriodo.setText("Ano de Ingresso/Perírodo: ");

        panelDic.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Disciplinas Matriculadas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        cBoxDisc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cBoxDiscActionPerformed(evt);
            }
        });

        labelNota1.setText("1ª Nota: ");

        labelNota2.setText("2ª Nota: ");

        labelNota3.setText("3ª Nota: ");

        labelNota4.setText("4ª Nota: ");

        labelFinal.setText("Prova Final: ");

        labelFaltas.setText("Faltas: ");

        labelCH.setText("Carga Horária: ");

        labelMedia.setText("Média Atual: ");

        labelSituacao.setText("<html>Siutação:<br> ");

        javax.swing.GroupLayout panelDicLayout = new javax.swing.GroupLayout(panelDic);
        panelDic.setLayout(panelDicLayout);
        panelDicLayout.setHorizontalGroup(
            panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cBoxDisc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(panelDicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNota3)
                    .addComponent(labelNota4)
                    .addComponent(labelFinal)
                    .addComponent(labelFaltas)
                    .addComponent(labelCH)
                    .addGroup(panelDicLayout.createSequentialGroup()
                        .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelNota1)
                            .addComponent(labelNota2))
                        .addGap(129, 129, 129)
                        .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelMedia))))
                .addContainerGap())
        );
        panelDicLayout.setVerticalGroup(
            panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDicLayout.createSequentialGroup()
                .addComponent(cBoxDisc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelCH)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNota1)
                    .addComponent(labelMedia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNota2)
                    .addComponent(labelSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelNota3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelNota4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFinal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFaltas)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelDic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelNome)
                            .addComponent(labelCurso)
                            .addComponent(labelAnoPeriodo))
                        .addGap(0, 354, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelCurso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAnoPeriodo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cBoxDiscActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cBoxDiscActionPerformed
        // TODO add your handling code here:
        if (cBoxDisc.getActionCommand().equals("comboBoxChanged") && cBoxDisc.isEnabled()) {
            if (cBoxDisc.getSelectedIndex() != -1) {
                disciplina = disciplinas.get(cBoxDisc.getSelectedIndex());
                labelCH.setText(labelCH.getText() + disciplina.getCh());
                labelFaltas.setText(labelFaltas.getText() + disciplina.getFaltasAluno(aluno.getMatricula()));
                notas = disciplina.getNotasAluno(aluno.getMatricula());
                sets = disciplina.getSetsAluno(aluno.getMatricula());
                if (sets[0]) {
                    soma += notas[0];
                    qtdenotas++;
                    labelNota1.setText("1ª Nota: " + decformat.format(notas[0]));
                }else{
                    labelNota1.setText("1ª Nota: --");
                }
                if (sets[1]) {
                    soma += notas[1];
                    qtdenotas++;
                    labelNota2.setText("2ª Nota: " + decformat.format(notas[1]));
                }else{
                    labelNota2.setText("2ª Nota: --");
                }
                if (disciplina.getQtdeNotas() > 3) {
                    if (sets[2]) {
                        soma += notas[2];
                        qtdenotas++;
                        labelNota3.setText("3ª Nota: " + decformat.format(notas[2]));
                    }else{
                        labelNota3.setText("3ª Nota: --");
                    }
                    if (disciplina.getQtdeNotas() > 4) {
                        if (sets[3]) {
                            soma += notas[3];
                            qtdenotas++;
                            labelNota4.setText("4ª Nota: " + decformat.format(notas[3]));
                        }else{
                            labelNota4.setText("4ª Nota: --");
                        }
                    }else{
                        labelNota4.setText("4ª Nota: --");
                    }
                }else{
                    labelNota3.setText("3ª Nota: --");
                    labelNota4.setText("4ª Nota: --");
                }
                if(soma == 0 && qtdenotas == 0){
                    labelSituacao.setText("Situação: sem notas");
                    labelMedia.setText("Média: --");
                    media = 0;
                }else{
                    media = soma/qtdenotas;
                    labelMedia.setText("Média: " + decformat.format(media));
                }
                // veirifica se foram colocadas todas as notas antes da final
                boolean ok = true;
                for(int i = 0 ; i < disciplina.getQtdeNotas()-1; i++){
                    ok = ok && sets[i];
                }
                if(ok){
                    //verifica se a nota final foi setada
                    if(sets[disciplina.getQtdeNotas()-1]){
                        labelFinal.setText("Nota da Final: " + decformat.format(notas[disciplina.getQtdeNotas()-1]));
                        double mediafinal = (media*6 + notas[disciplina.getQtdeNotas()-1]*4)/10;
                        labelMedia.setText("Média: " + mediafinal);
                        if(mediafinal > 5){
                            labelSituacao.setText("Situação: Aprovado");
                        }
                    }else{
                        if(media < 4){
                            labelSituacao.setText("Situação: Reprovado por media < 4");
                        }
                        if(media >= 7){
                            labelSituacao.setText("Situação: Aprovado.");
                        }
                        if(media >= 4 && media < 7){
                            double notaNaFinal;
                            notaNaFinal = (50 - 6*media)/4;
                            labelSituacao.setText("Situação: Na final precisando de " + decformat.format(notaNaFinal));
                        }
                    }
                    
                }
                if(!ok){
                    labelFinal.setText("Nota da Final: --");
                }
                if(disciplina.getFaltasAluno(aluno.getMatricula()) >= disciplina.getCh()*0.25 ){
                    labelSituacao.setText("Situação: Reprovado por falta");
                }
                
            }
        }
    }//GEN-LAST:event_cBoxDiscActionPerformed
    private LinkedList<Disciplina> disciplinas = new LinkedList<Disciplina>();
    private Disciplina disciplina;
    private Aluno aluno;
    private double notas[], soma = 0, media = 0, qtdenotas = 0;
    private boolean[] sets;
    DecimalFormat decformat = new DecimalFormat("0.00");
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cBoxDisc;
    private javax.swing.JLabel labelAnoPeriodo;
    private javax.swing.JLabel labelCH;
    private javax.swing.JLabel labelCurso;
    private javax.swing.JLabel labelFaltas;
    private javax.swing.JLabel labelFinal;
    private javax.swing.JLabel labelMedia;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelNota1;
    private javax.swing.JLabel labelNota2;
    private javax.swing.JLabel labelNota3;
    private javax.swing.JLabel labelNota4;
    private javax.swing.JLabel labelSituacao;
    private final javax.swing.JPanel panelDic = new javax.swing.JPanel();
    // End of variables declaration//GEN-END:variables
}
